package com.ch999.mobileoa.request;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ch999.mobileoa.MyApplication;
import com.ch999.mobileoa.data.Result;
import com.ch999.mobileoa.data.SearchData;
import com.ch999.mobileoa.data.UserData;
import com.google.gson.Gson;
import com.sda.lib.HttpCore.DataResponse;
import com.sda.lib.HttpCore.MyHttp;
import com.sda.lib.HttpCore.RequestParams;
import com.sda.lib.HttpCore.TextHandler;
import com.sda.lib.util.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by scorpio on 15/12/23.
 */
public class DataControl {


    /**
     * 返回数据成功
     *
     * @param json_temp
     * @return
     */
    private static boolean isSucc(JSONObject json_temp) {
        if (json_temp.getInteger("stats") == 1) {
            return true;
        }
        return false;
    }

    /**
     * @param context
     * @param name
     * @param pwd
     * @param dr
     */
    public static void Login(Context context, final String name, final String pwd, final DataResponse dr) {
        RequestParams params = new RequestParams();
        params.put("user", name);
        params.put("pwd", Tools.md5(pwd));
        params.put("appidentifier", Tools.getIMEI(context));
        MyHttp.post(context, "login", params, new TextHandler() {
            @Override
            public void onSuccess(int header, Object response) {
                Result result = Result.parse(response);
                if (result.isSucc()) {
                    UserData userData = new Gson().fromJson(result.getData(), UserData.class);
                    userData.setPwd(Tools.md5(pwd));
                    userData.setUser(name);
                    dr.onSucc(userData);
                } else {
                    dr.onFail(result.getMsg());
                }

            }

            @Override
            public void onFailure(Throwable error) {

            }
        });
    }


    /**
     * APP 考情打卡
     *
     * @param url
     * @param dr
     */
    public static void AppDaka(Context context , String url, final DataResponse dr) {
        RequestParams params = new RequestParams();
        if (Tools.isEmpty(MyApplication.gps)){
            dr.onFail("定位失败,请开启GPS 定位功能");
            return;
        }
        params.put("gprs",MyApplication.gps);
        params.put("appidentifier", Tools.getIMEI(context));
        MyHttp.get(context, url, params, new TextHandler() {
            @Override
            public void onSuccess(int header, Object response) {
                Result result = Result.parse(response);
                if (result.isSucc()) {
                    dr.onSucc("");
                } else {
                    dr.onFail(result.getMsg());
                }

            }

            @Override
            public void onFailure(Throwable error) {

            }
        });
    }


    /**
     * 快速搜索
     * @param context
     * @param key
     * @param dr
     */
    public static void FuzzyQuery(Context context , String key, final DataResponse dr) {
        RequestParams params = new RequestParams();
        params.put("key",key);
        MyHttp.get(context, "FuzzyQuery", params, new TextHandler() {
            @Override
            public void onSuccess(int header, Object response) {
                Result result = Result.parse(response);
                if (result.isSucc()) {
                    JSONArray jsonArray = JSON.parseArray(result.getData());
                    List<SearchData>list = new ArrayList<SearchData>();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        SearchData data = new Gson().fromJson(jsonArray.getJSONObject(i).toString(),SearchData.class);
                        list.add(data);
                    }
                    dr.onSucc(list);
                } else {
                    dr.onFail(result.getMsg());
                }

            }

            @Override
            public void onFailure(Throwable error) {

            }
        });
    }
}
